package com.daily.pump.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.daily.pump.R;
import com.daily.pump.app.App;
import com.daily.pump.model.Device;
import com.xunxi.mimi.utils.ToastUtil;


/**
 * Desc:我的-设置视频聊天价格
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class ModifyPwdDialog extends DialogFragment {

    private ScanDialogListener mScanDialogListener;

    private TextView mNameEt;

    private EditText mOldPwdEt;

    private EditText mNewPwdEt;

    private ProgressBar mProgressBar;

    public void setScanDialogListener(ScanDialogListener listener) {
        this.mScanDialogListener = listener;
    }

    public static ModifyPwdDialog newInstance() {
        ModifyPwdDialog loginDialog = new ModifyPwdDialog();
        return loginDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismissAllowingStateLoss();
                if (mScanDialogListener != null) {
                    mScanDialogListener.onDismiss();
                }
            }
        });
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_modfy_pwd, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        mNameEt = dialogView.findViewById(R.id.name_et);
        mOldPwdEt = dialogView.findViewById(R.id.old_pwd_et);
        mNewPwdEt = dialogView.findViewById(R.id.new_pwd_et);
        mProgressBar = dialogView.findViewById(R.id.progress);
        dialogView.findViewById(R.id.modify_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyPwd();
            }
        });
        mNameEt.setText("CUSTOM");
    }

    private void modifyPwd() {
        String name = mNameEt.getText().toString().trim();
        String oldPwd = mOldPwdEt.getText().toString().trim();
        String newPwd = mNewPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.show(getActivity(), "please input device name!");
            return;
        }

        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtil.show(getActivity(), "please input old password");
            return;
        }

        if (TextUtils.isEmpty(newPwd)) {
            ToastUtil.show(getActivity(), "please input new password");
            return;
        }

        if (oldPwd.length() < 3){
            ToastUtil.show(getActivity(), "password at least 3 characters");
            return;
        }

        if (newPwd.length() < 3){
            ToastUtil.show(getActivity(), "password at least 3 characters");
            return;
        }


        if (mScanDialogListener != null){
            mScanDialogListener.onModifyPwd(name, oldPwd, newPwd);
        }
        mProgressBar.setVisibility(View.VISIBLE);
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, ModifyPwdDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    /**
     * Desc:隐藏进度条
     * <p>
     * Author: [李豫]
     * Date: 2020-11-13
     */
    public void hideProgress() {
        if (mProgressBar != null){
            mProgressBar.setVisibility(View.GONE);
        }
    }

    /**
     * Desc:修改登录密码
     * <p>
     * Author: [李豫]
     * Date: 2020-11-13
     */
    public void modifyLoginPwd() {
        String newPwd = mNewPwdEt.getText().toString().trim();
        Device loginDevice = App.getInstance().getLoginDevice();
        loginDevice.password = newPwd;
        App.getInstance().setLoginDevice(loginDevice);
    }

    public interface ScanDialogListener {
        void onModifyPwd(String deviceName, String oldPwd, String newPwd);

        void onDismiss();
    }
}

